﻿using Domain.Entitys.DB;
using Domain.IRepositorys.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys.DB
{
    public class Db_MeasureRepository:IDb_MeasureRepository
    {
        private readonly MyDbContext myDbContext;

        public Db_MeasureRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        /// <summary>
        /// 添加单位
        /// </summary>
        /// <param name="db_Measure"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Db_MeasureAdd(Db_Measure db_Measure)
        {
            await myDbContext.db_Measure.AddRangeAsync(db_Measure);
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 查询总条数
        /// </summary>
        /// <param name="Measure_code">编码</param>
        /// <param name="Measure_name">昵称</param>
        /// <returns></returns>
        public int Db_MeasureCount(string Measure_code, string Measure_name)
        {
            //逻辑删除
            IQueryable<Db_Measure> query = myDbContext.db_Measure.Where(d => d.Del_flag == '0');
            //条件查询
            if (!string.IsNullOrEmpty(Measure_code))
            {
                query = query.Where(d => d.Measure_code == Measure_code);
            }
            if (!string.IsNullOrEmpty(Measure_name))
            {
                query = query.Where(d => d.Measure_name == Measure_name);
            }
            return query.Count();
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="db_Measure"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Db_MeasureDelete(Db_Measure db_Measure)
        {
            Db_Measure db = await myDbContext.db_Measure.FindAsync(db_Measure.Measure_id);
            db.Del_flag = db_Measure.Del_flag;
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 修改计量单位
        /// </summary>
        /// <param name="db_Measure"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Db_MeasureUpdate(Db_Measure db_Measure)
        {
            Db_Measure db = await myDbContext.db_Measure.FindAsync(db_Measure.Measure_id);
            db.Measure_code = db_Measure.Measure_code;
            db.Measure_name = db_Measure.Measure_name;
            db.Measure_host = db_Measure.Measure_host;
            db.Measure_scale = db_Measure.Measure_scale;
            db.Measure_enable = db_Measure.Measure_enable;
            db.Remark = db_Measure.Remark;
            db.Update_by = db_Measure.Update_by;
            db.Update_time = db_Measure.Update_time;
            myDbContext.db_Measure.Update(db);
            return await myDbContext.SaveChangesAsync();
        }
        //public async Task<int> Db_MeasureUpdate(Db_Measure db_Measure)
        //{
        //    if (db_Measure == null)
        //    {
        //        throw new ArgumentNullException(nameof(db_Measure), "The provided db_Measure object is null.");
        //    }

        //    Db_Measure db = await myDbContext.db_Measure.FindAsync(db_Measure.Measure_id);

        //    if (db == null)
        //    {
        //        throw new InvalidOperationException($"No record found with Measure_id: {db_Measure.Measure_id}");
        //    }

        //    db.Measure_code = db_Measure.Measure_code ?? db.Measure_code; // 防止null值
        //    db.Measure_name = db_Measure.Measure_name ?? db.Measure_name; // 防止null值
        //    db.Measure_host = db_Measure.Measure_host; // 防止null值
        //    db.Measure_scale = db_Measure.Measure_scale; // 防止null值
        //    db.Measure_enable = db_Measure.Measure_enable; // 防止null值
        //    db.Remark = db_Measure.Remark ?? db.Remark; // 防止null值
        //    db.Update_by = db_Measure.Update_by ?? db.Update_by; // 防止null值
        //    db.Update_time = db_Measure.Update_time ?? db.Update_time; // 防止null值

        //    myDbContext.db_Measure.Update(db);

        //    return await myDbContext.SaveChangesAsync();
        //}



        /// <summary>
        /// 根据条件、分页查询计量单位信息
        /// </summary>
        /// <param name="Measure_code">编码</param>
        /// <param name="Measure_name">姓名</param>
        /// <param name="currentPage">当前页面</param>
        /// <param name="pageSize">查几条</param>
        /// <returns></returns>
        public List<Db_Measure> SelectDb_MeasureFYWHERE(string Measure_code, string Measure_name, int currentPage, int pageSize)
        {
            //判断逻辑删除
            IQueryable<Db_Measure> query = myDbContext.db_Measure.Where(d => d.Del_flag == '0');
            //条件查询
            if (!string.IsNullOrEmpty(Measure_code))
            {
                query = query.Where(d => d.Measure_code == Measure_code);
            }
            if (!string.IsNullOrEmpty(Measure_name))
            {
                query = query.Where(d => d.Measure_name == Measure_name);
            }
            //分页查询
            var Db_Measure = query.Skip((currentPage-1)* pageSize).Take(pageSize).ToList();
            return Db_Measure;
        }
    }
}
